import { constantRoutes, asyncRoutes } from '@/router'
import store from '@/store'
const state = {
  routes: []
}
const mutations = {
  setRoutes(state, data) {
    state.routes = [...constantRoutes, ...data]
  }
}
const actions = {
  filterRoutes() {
    const menus = store.state.user.userInfo.roles.menus
    const routes = asyncRoutes.filter(item => menus.includes(item.name))
    // 本来,这个逻辑实在导航守卫的,封装在这里是为了同时在俩个逻辑上
    // 1.回复url访问(靠return到导航守卫调用router.addRoutes)
    // 2.希望拼接出一个菜单列表,替换原来默认的路由配置(调用mutation)
    store.commit('permission/setRoutes', routes)
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
